const app = getApp()
import http from '../../../utils/http'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        nameKey: "",
        nutrientContent: [],
        foodInfo: null,
        foodContent:[],
        addContent: false,
        contentAmount: 0,
        contentId: "",
        contentName: "",


        name: "",
        description: "",
        pictureUrl: ""
    },
    onLoad: function (options) {
        http({
            url: '/food/query/' + options.id,
            success: (result) => {
                console.log(result)
                this.setData({
                    foodInfo: result.info,
                    name: result.info.name,
                    description: result.info.description,
                    pictureUrl: result.info.picture,
                    foodContent:result.info.contentList
                })
            }
        })

        http({
            url: '/ingredient/adminPageOrder/1/20',
            method: "post",
            data: {
                nameKey: "小麦"
            },
            success: (result) => {
                console.log(result)
                this.setData({
                    nutrientContent: result.rows
                })
            }
        })
    },
    showAddContent(e) {
        console.log(e)
        this.setData({
            addContent: true,
            contentId: e.currentTarget.dataset.item.ncId,
            contentName: e.currentTarget.dataset.item.ncName,
        })
    },

    confirmAddContent() {
        var that = this;
        var temp = this.data.foodContent;
        temp.push({
            id: that.data.contentId,
            name: that.data.contentName,
            amount: that.data.contentAmount
        })
        this.setData({
            addContent: false,
            foodContent: temp
        })
    },

    candelAddContent() {
        this.setData({
            addContent: false
        })
    },

    removeContent(e) {
        var that = this;
        var temp = this.data.foodContent;
        temp.splice(e.currentTarget.dataset.index, 1)
        this.setData({
            foodContent: temp
        })
    },

    searchContent(e) {
        http({
            url: '/ingredient/adminPageOrder/1/5',
            method: "post",
            data: {
                nameKey: e.detail.value
            },
            success: (result) => {
                console.log(result)
                this.setData({
                    nutrientContent: result.rows
                })
            }
        })
    },

    // 获取图片路径
    locadPicture: function () {
        let that = this;
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function (res) {
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                var tempFilePaths = res.tempFilePaths
                that.setData({
                    pictureUrl: tempFilePaths[0]
                })
            }
        })
    },

    updateFood() {
        var that = this
        // 图片上传
        if (that.data.pictureUrl == that.data.foodInfo.picture) {
            http({
                url: '/food/update',
                method: "post",
                data: {
                    id: that.data.foodInfo.id,
                    name: that.data.name,
                    picture: that.data.pictureUrl,
                    description: that.data.description,
                    contentList: that.data.foodContent,
                },
                success: (result) => {
                    console.log(result)
                    wx.showModal({
                        title: '提示',
                        content: '修改成功！',
                        success(res) {
                            if (res.confirm) {
                                wx.switchTab({url:'../../food/food'});  
                            } else if (res.cancel) {

                            }
                        }
                    })
                }
            })
        } else {
            wx.showLoading({
                title: '加载中',
                mask: true
            })
            wx.uploadFile({
                url: 'https://www.zzuexpress.xyz/express/api/oss',
                filePath: that.data.pictureUrl,
                name: 'file',
                header: {
                    'content-type': 'application/json',
                    'Cookie': 'JSESSIONID=' + app.globalData.sessionId
                },
                formData: {
                    'user': 'test'
                },
                success: function (res) {
                    wx.hideLoading();
                    console.log(res)
                    let url = JSON.parse(res.data).data.url
                    http({
                        url: '/food/update',
                        method: "post",
                        data: {
                            id: that.data.foodInfo.id,
                            name: that.data.name,
                            picture: url,
                            description: that.data.description,
                            contentList: that.data.foodContent,
                        },
                        success: (result) => {
                            console.log(result)
                            wx.showModal({
                                title: '提示',
                                content: '修改成功！',
                                success(res) {
                                    if (res.confirm) {
                                        wx.switchTab({url:'../../food/food'});  
                                    } else if (res.cancel) {

                                    }
                                }
                            })
                        }
                    })
                },
                fail: function (res) {
                    wx.hideLoading();
                    wx.showModal({
                        title: '提示',
                        content: '操作失败，请稍后重试',
                    })
                }
            })
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    bindinput() {},
})